<?php

/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/16
 * Time: 13:40
 */
namespace Bbs\Api;

use Bbs\Model\ForumForumModel;
use Common\Api\Api;

class ForumForumApi extends Api{

    /**
     * 添加
     */
    const ADD = "Bbs/ForumForum/add";
    /**
     * 保存
     */
    const SAVE = "Bbs/ForumForum/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Bbs/ForumForum/saveByID";

    /**
     * 删除
     */
    const DELETE = "Bbs/ForumForum/delete";

    /**
     * 查询
     */
    const QUERY = "Bbs/ForumForum/query";

    /**
     * 查询不分页
     */
    const QUERY_NO_PAGING = "Bbs/ForumForum/queryNoPaging";

    /**
     * 查询一条数据
     */
    const GET_INFO = "Bbs/ForumForum/getInfo";

    /**
     * 分页查询，带上一条主题
     */
    const QUERY_WITH_THREAD = "Bbs/ForumForum/queryWithThread";

    const QUERY_WITH_COUNT = "Bbs/ForumForum/queryWithCount";
    /**
     * 所有板块
     */
    const QUERY_WITH_CHILD = "Bbs/ForumForum/queryWithChild";
    const GET_IDS = "Bbs/ForumForum/getIds";

    /**
     * 添加时候更改版块内容
     */
    const UPDATE = "Bbs/ForumForum/update";

    const SET_DEC = "Bbs/ForumForum/setDec";

    /**
     * 统计
     */
    const COUNT = "Bbs/ForumForum/count";


    protected function _init(){
        $this -> model = new ForumForumModel();
    }
    /**
     * 获取板块ids
     * @param  [type] $fid 不为0
     * @return [type]      [description]
     */
    public function getIds($fid){
        $str = $fid;
        $rest = $this->model->where(array('fup'=>$fid))->select();
        if($rest){
            foreach ($rest as $v) {
                $str .=','.$v['fid'];
            }
        }
        return $str;
    }
    public function queryWithChild(){
        $rest = $this->model->where(array('fup'=>0))->select();
        foreach ($rest as &$v) {
            $v['child'] = $this->model->where(array('fup'=>$v['fid']))->select();
        }
        return $this -> apiReturnSuc($rest);
    }
    public function queryWithThread($map = null, $page = array('curpage'=>1,'size'=>6), $order = false, $params = false, $fields = false){

        $query = $this -> model;

        $query = $query -> join(" (select ft.dateline,ft.author,ft.author_id,ft.replies,ft.likes,ft.fid,ft.subject,ft.tid from (select dateline,author,author_id,replies,likes,fid,subject,tid from itboye_forum_thread where status=1 order by dateline desc) as ft group by fid) as thread on itboye_forum_forum.fid=thread.fid","left");

        $query = $query -> page($page['curpage'] . ',' . $page['size']);

        if($map !== null){
            $query = $query -> where($map);
        }
        if($order !== false){
            $query = $query -> order($order);
        }
        if($fields !== false){
            $query = $query -> field($fields);
        }
        $list = $query -> select();

        if($list === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));

    }


    public function update($fid , $author , $thread = false){

        $map = array(
            'fid' => $fid,
        );
        $entity = array(
            'posts' => array('exp',"posts+1"),
            'lastpost' => $author,
        );
        if($thread !== false){
            $entity['threads'] = array('exp',"threads+1");
        }
        $result = $this -> model -> create($entity, 2);
        if($result === false){
            $error = $this -> model -> getError();
            return $this -> apiReturnErr($error);
        }
        $result = $this -> model -> where($map) -> save();
        if ($result === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        } else {
            return $this -> apiReturnSuc($result);
        }

    }

}